// pages/review/create.js
const reviewService = require('../../services/review');
const appointmentService = require('../../services/appointment');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appointmentId: null,       // 预约ID
    appointment: null,         // 预约信息
    score: 5,                  // 默认评分为5
    content: '',               // 评价内容
    isLoading: true,           // 是否正在加载
    isSubmitting: false,       // 是否正在提交
    error: null,               // 错误信息
    stars: [1, 2, 3, 4, 5]     // 五颗星
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('评价页面加载，参数：', options);
    
    if (!options.id) {
      this.setData({
        isLoading: false,
        error: '缺少预约ID'
      });
      return;
    }
    
    this.setData({
      appointmentId: options.id
    });
    
    this.loadAppointment();
  },

  /**
   * 加载预约信息
   */
  loadAppointment: function () {
    console.log('开始加载预约信息:', this.data.appointmentId);
    this.setData({ isLoading: true, error: null });
    
    appointmentService.getAppointmentDetail(this.data.appointmentId)
      .then(appointment => {
        console.log('获取到预约信息:', appointment);
        
        // 检查预约状态是否已完成
        if (appointment.status !== 2) {
          this.setData({
            isLoading: false,
            error: '只能评价已完成的预约'
          });
          return;
        }
        
        // 检查是否已评价
        return reviewService.getReviewByAppointmentId(this.data.appointmentId)
          .then(review => {
            if (review) {
              this.setData({
                isLoading: false,
                error: '该预约已评价，不能重复评价'
              });
            } else {
              this.setData({
                appointment: appointment,
                isLoading: false
              });
            }
          });
      })
      .catch(err => {
        console.error('加载预约信息失败:', err);
        this.setData({
          isLoading: false,
          error: err.message || '加载预约信息失败'
        });
      });
  },

  /**
   * 选择评分
   */
  selectScore: function (e) {
    const score = e.currentTarget.dataset.score;
    console.log('选择评分:', score);
    this.setData({ score });
  },

  /**
   * 输入评价内容
   */
  inputContent: function (e) {
    this.setData({ content: e.detail.value });
  },

  /**
   * 提交评价
   */
  submitReview: function () {
    if (this.data.isSubmitting) {
      return;
    }
    
    // 检查评分
    if (this.data.score < 1 || this.data.score > 5) {
      wx.showToast({
        title: '评分范围为1-5',
        icon: 'none'
      });
      return;
    }
    
    // 确保有appointment数据且包含staffId
    if (!this.data.appointment || !this.data.appointment.staffId) {
      wx.showToast({
        title: '获取医生信息失败，请重试',
        icon: 'none'
      });
      return;
    }
    
    this.setData({ isSubmitting: true });
    
    const reviewData = {
      appointmentId: this.data.appointmentId,
      staffId: this.data.appointment.staffId,
      score: this.data.score,
      content: this.data.content
    };
    
    console.log('准备提交评价:', reviewData);
    
    reviewService.addReview(reviewData)
      .then(res => {
        console.log('评价提交成功:', res);
        wx.showToast({
          title: '评价成功',
          icon: 'success'
        });
        
        // 延迟返回，让用户看到成功提示
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      })
      .catch(err => {
        console.error('评价提交失败:', err);
        this.setData({ isSubmitting: false });
        wx.showToast({
          title: err.message || '评价提交失败',
          icon: 'none'
        });
      });
  },

  /**
   * 返回上一页
   */
  navigateBack: function () {
    wx.navigateBack();
  }
}); 